<?php
namespace app\admin\controller;

use app\BaseController;
use think\facade\Log;
use think\facade\View;

class Login extends BaseController {


    function initialize()
    {
        parent::initialize();

        $check = $this->request->checkToken('__token__');

        if(false === $check) {
            return show(config("status.error"), "token无效");
        }
    }

    /**
     *  登录页面
     * @return string
     */
    public function index() {

        return View::fetch();
    }

    public function check() {

        try{
            if (!$this->request->isPost()) {
                return show(config("status.error"), "请求方式错误");
            }

            // 获取参数
            $username = $this->request->param("username", "", "trim");
            $password = $this->request->param("password", "", "trim");
            $captcha = $this->request->param("captcha", "", "trim");

            $data = [
                "username" => $username,
                "password" => $password,
                "captcha" => $captcha,
            ];
            // 参数校验
            $validate = new \app\admin\validate\Admin();
            if (!$validate->check($data)) {
                return show(config("status.error"), $validate->getError());
            }

            // 判断是否登录成功
            $business = new \app\admin\business\Admin();
            if (!$business->login($data)) {
                return show(config("status.error"), $business->getError());
            }

            return show(config("status.success"), "登录成功");
        }catch (\Exception $e) {
            Log::log(Log::ERROR, $e->getMessage());
            return show(config("status.error"), "内部异常, 登录失败");
        }
    }


}